package edu.friday.controller;

import edu.friday.common.constant.Constants;
import edu.friday.common.result.RestResult;
import edu.friday.common.security.LoginUser;
import edu.friday.common.security.service.MyTokenService;
import edu.friday.common.security.service.SysLoginService;
import edu.friday.common.security.service.SysPermissionService;
import edu.friday.model.vo.SysUserVo;
import edu.friday.service.SysUserService;
import edu.friday.utils.http.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private MyTokenService myTokenService;

    @Autowired
    private SysPermissionService sysPermissionService;

    @PostMapping({"/login","/"})
    public RestResult login(String username, String password,String uuid){
        RestResult ajax=RestResult.success();
        String token=loginService.login(username,password,"123",uuid);
        ajax.put(Constants.TOKEN,token);
        return ajax;
    }


    @GetMapping("/getInfo")
    public RestResult getInfo(){
        LoginUser loginUser=myTokenService.getLoginUser(ServletUtils.getRequest());
        SysUserVo user=loginUser.getUser();


        Set<String> permissions=sysPermissionService.getMenuPermission(user);
        Set<String> roles=sysPermissionService.getRolePermission(user);

        RestResult ajax=RestResult.success();
        ajax.put("user",user);
        ajax.put("roles",roles);
        ajax.put("permissions",permissions);
        return ajax;
    }

}
